import { toJS, observable, action } from 'mobx';
import { WeaTools } from 'ecCom';
import * as Apis from '../apis/sellChance';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class FormTabStore {
    comObj;
    sellChanceId;
    @observable verified = false;
    @observable hasRight = false;
    @observable tabDatas = [];
    @observable selectedKey = '';
    @observable sellChanceName = '';
    @observable pathLocation ={}

    @action
    componentWillMount = (comObj) => {
      this.comObj = comObj;
      this.sellChanceId = this.comObj.props.params.sellChanceId;
      if ('location' in this.comObj.props) {
        this.pathLocation = this.comObj.props.location.query;
      }
    }

    @action
    componentDidMount = () => {
      // const requestParams = {sellChanceId:this.sellChanceId};
      let requestParams = {};
      if (typeof (this.pathLocation.moduleid) !== 'undefined') {
        requestParams = { ...this.pathLocation };
      } else {
        requestParams = { sellchanceId: this.sellChanceId };
      }
      Apis.getSellChanceTabs(requestParams).then((data) => {
        this.componentInitCallBack(data);
      });
    }

    @action
    componentInitCallBack = (data) => {
      this.verified = true;
      this.hasRight = data.hasright;
      if (this.hasRight) {
        this.tabDatas = data.datas;
        this.selectedKey = data.selectedKey;
        this.sellChanceName = data.sellChanceName;
      }
    }

    @action
    componentWillUnmount = () => {

    }

    @action
    changeTab(key) {
      this.selectedKey = key;
    }

    @action
    extendHandle =(key, title, url, shortname) => {
      const _tabDatas = toJS(this.tabDatas);
      let finded = false;
      for (const tab of _tabDatas) {
        if (tab.key == key) {
          finded = true;
          break;
        }
      }
      if (!finded) {
        if (typeof (shortname) === 'undefined') {
          shortname = '';
        }
        _tabDatas.push({ id: key, key, title, mainTitle: title, linkurl: url, shortname, editable: true });
        this.tabDatas = _tabDatas;
      }
      this.changeTab(key);
    }

    @action
    onTabEdit = (tabKey, type) => {
      const _tabDatas = toJS(this.tabDatas);
      if (type === 'remove') {
        this.tabDatas = _tabDatas.filter(t => t.key != tabKey);
        this.changeTab(_tabDatas[0].key);
      }
    }
}
